package com.huobao.yunying.adapters;

import com.huobao.yunying.entity.Coupon_list;
import com.huobao.yunying.entity.JiesuanData;
import com.huobao.yunying.entity.Jiesuan_goods_info;
import com.huobao.yunying.utils.PopupYouhui;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Adapter_Jiesuan_ extends QuickAdapter<Jiesuan_goods_info>{

	private TextView tv_allPrice;
	private JiesuanData jiesuanData; 
	public Adapter_Jiesuan_(Context context,TextView tv_allPrice) {
		super(context, R.layout.item_jiesuan_);
		this.tv_allPrice = tv_allPrice;
	}
	
	public void setJiesuanData(JiesuanData jiesuanData){
		this.jiesuanData = jiesuanData;
		notifyDataSetChanged();
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final Jiesuan_goods_info data) {
		EditText edt_liuyan = helper.getView(R.id.edt_liuyan);
		data.liuyan_edt = edt_liuyan;
		TextView tv_storename = helper.getView(R.id.tv_storename);
		TextView tv_store_address = helper.getView(R.id.tv_store_address);
		TextView tv_price = helper.getView(R.id.tv_price);
		tv_storename.setText(data.store_name);
		tv_price.setText("￥"+(Float.parseFloat(data.store_amount)+Float.parseFloat(data.store_total_freight)));
		tv_store_address.setText(data.address);
		
		LinearLayout layout = helper.getView(R.id.llyt);
		
		NoScrollListView listview = helper.getView(R.id.listview);
		Adapter_jiesuan_item adapter = new Adapter_jiesuan_item(context,data.goods);
		listview.setAdapter(adapter);
		//配送价格
		TextView tv_peisong_price = helper.getView(R.id.tv_peisong_price);
		tv_peisong_price.setText("￥"+data.store_total_freight);
		
		final NoScrollListView lv_peisong = helper.getView(R.id.listview_peisong);
		final Adapter_Peisong adapter_Peisong = new Adapter_Peisong(context, data.shipping_methods, data.store_freight, Float.parseFloat(data.store_total_freight),data,Adapter_Jiesuan_.this);
		lv_peisong.setAdapter(adapter_Peisong);
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(lv_peisong.getVisibility() == View.VISIBLE){
					lv_peisong.setVisibility(View.GONE);
				}else{
					lv_peisong.setVisibility(View.VISIBLE);
					//配送的列表
				}
			}
		}); 
		
		//优惠券信息
		final TextView tv_youhuiquan = helper.getView(R.id.tv_youhuiquan);
		View view_youhuiquan = helper.getView(R.id.llyt_youhui);
		if(jiesuanData.allow_coupon && data.coupon_list != null && data.coupon_list.size() != 0){
			for(Coupon_list coupon_list : data.coupon_list){
				coupon_list.isSelect = false;
			}
			Coupon_list select_Coupon = data.coupon_list.get(0);
			select_Coupon.isSelect = true;
			notifyDataSetChanged();
			tv_youhuiquan.setText(select_Coupon.coupon_name+" "+select_Coupon.coupon_value);
			view_youhuiquan.setVisibility(View.VISIBLE);
			PopupYouhui.showPop(context, data.coupon_list, tv_youhuiquan, new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					PopupYouhui.dismiss();
					for(Coupon_list coupon_list : data.coupon_list){
						coupon_list.isSelect = false;
					}
					Coupon_list m_Select_Coupon = data.coupon_list.get(0);
					m_Select_Coupon.isSelect = true;
					tv_youhuiquan.setText(m_Select_Coupon.coupon_name+" "+m_Select_Coupon.coupon_value);
					notifyDataSetChanged();
				}
			});
		}else{
			view_youhuiquan.setVisibility(View.GONE);
		}
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		float allPrice = 0;
		for(Jiesuan_goods_info data : getList()){
			allPrice += (Float.parseFloat(data.store_amount)+Float.parseFloat(data.store_total_freight));
			float youhui = 0;
			if(jiesuanData.allow_coupon && data.coupon_list != null && data.coupon_list.size() == 0){
				for(Coupon_list coupon_list : data.coupon_list){
					if(coupon_list.isSelect){
						youhui = Float.parseFloat(coupon_list.coupon_value);
					}
				}
				allPrice -= youhui;
			}
		}
		tv_allPrice.setText(allPrice+"");
	}

}
